package webjoch.process.strategies.puzzles;

import java.awt.Graphics2D;

import com.rsbuddy.event.events.MessageEvent;
import com.rsbuddy.script.methods.Objects;
import com.rsbuddy.script.methods.Players;
import com.rsbuddy.script.wrappers.GameObject;
import com.rsbuddy.script.wrappers.Tile;

import webjoch.misc.Functions;
import webjoch.process.strategies.Puzzle;

public class IcePad extends Puzzle{
	public final static int[] PADS = {49320, 49321, 49322, 49322};
	int rounds = 0;
	int minDirection = 0;
	int onRound = 0;
	
	@Override
	public boolean isValid() {
		return getPad() != null;
	}

	@Override
	public Type getType() { 
		return Type.ICE_PAD;
	}

	@Override
	public int loop(){
		System.out.println("---------");
		findPath(Players.getLocal().getLocation());
		return 0;
	}
	
	public boolean findPath(Tile location){
		int x = location.getX();
		int y = location.getY();
		Tile last = location;
		
		int beginDirection = 0;
		if (rounds == onRound){
			beginDirection = minDirection;
		}
		
		top:
		for (int direction = beginDirection; direction < 8; direction++){
			for (int i = 0; i < 100; i++){
				System.out.println(last);
				x += getTileWay(direction)[0] * 2;
				y += getTileWay(direction)[1] * 2;
				GameObject object = Objects.getTopAt(new Tile(x, y));
				if (object != null){
					//System.out.println(direction);
					rounds++;
					if (Functions.inArray(PADS, object.getId())) return true;
					
					if (rounds > 20){
						minDirection = minDirection >= 8 ? 0 : minDirection++;
						onRound++;
					}
					
					if (last.equals(location)) continue top;
					return findPath(last);
				}
				last = new Tile(x, y);
			}
		}
		return false;
	}

	public int[] getTileWay(int direction){
		int x = -1;
		int y = -1;
		for (int i = 0; i < 8; i++){
			if (i == direction){
				return new int[]{x, y};
			}
			if (x == 1){
				y++;
				x = -2;
			}
			x++;
			if (i == 3) x++;
		}
		return null;
	}
	
	@Override
	public int[] itemNeed() {
		return new int[0];
	}

	@Override
	public boolean isFinished() {
		return false;
	}

	@Override
	public void messageReceived(MessageEvent e) {
		// TODO Auto-generated method stub
		
	}

	public GameObject getPad(){
		return room.getObject(PADS);
	}
	
	public void onRepaint(Graphics2D g){
		g.drawString("test", 10, 100);
	}
}
